package com.blog.login.config;


import com.blog.login.shiro.AccountRealm;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;

import java.util.Map;

/**
 * @author admin
 * @version 1.0 <br/>
 * <p style="font-family:SimSun;font-weight:bold">时间:<br/>2021/12/8 16:15</p>
 * @description
 */
@Configuration
public class ShiroConfig {
    AccountRealm customerRealm=null;
    DefaultWebSecurityManager defaultWebSecurityManager=null;
    ShiroFilterFactoryBean shiroFilter=null;
    //EhCacheManager cacheManager=null;
    HashedCredentialsMatcher credentialsMatcher=null;
    //1.创建shiroFilter
    @Bean
    public ShiroFilterFactoryBean getShiroFilterBean(DefaultWebSecurityManager defaultWebSecurityManager){
        if(!ObjectUtils.isEmpty(shiroFilter))
            return null;
        shiroFilter=new ShiroFilterFactoryBean();
        //filter设置安全管理器
        shiroFilter.setSecurityManager(defaultWebSecurityManager);
        //配置系统的公共资源
        Map<String, String> map=new HashMap<>();
        shiroFilter.setFilterChainDefinitionMap(map);
        return shiroFilter;
    }
    //2.创建安全管理器
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(Realm realm){
        if(!ObjectUtils.isEmpty(defaultWebSecurityManager))
            return null;
        defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(realm);
        return defaultWebSecurityManager;
    }
    //3.创建realm
    @Bean
    public Realm getRealm(){
        if(!ObjectUtils.isEmpty( customerRealm))
            return null;
        customerRealm = new AccountRealm();
        //1.修改凭证匹配器
        if(!ObjectUtils.isEmpty(credentialsMatcher))
            return null;
        credentialsMatcher = new HashedCredentialsMatcher();
        //2.设置凭证匹配器的加密算法
        credentialsMatcher.setHashAlgorithmName("MD5");
        //3.设置散列次数
        credentialsMatcher.setHashIterations(1024);
        //2.添加匹配器到realm
        customerRealm.setCredentialsMatcher(credentialsMatcher);
       /* //1.开启缓存管理
        customerRealm.setCachingEnabled(true);
        //2.设置缓存管理器
        if(!ObjectUtils.isEmpty(cacheManager))
            return null;
        cacheManager = new EhCacheManager();
        customerRealm.setCacheManager(cacheManager);
        //3.开启认证和授权的缓存以及各自的名字
        customerRealm.setAuthenticationCachingEnabled(true);
        customerRealm.setAuthenticationCacheName("authentication");
        customerRealm.setAuthorizationCachingEnabled(true);
        customerRealm.setAuthorizationCacheName("authorization");*/
        return customerRealm;
    }
}
